package net.sf.xoperator.ui.txt;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.openbbs.blackboard.filter.KindOfFilter;

import net.sf.xoperator.blackboard.NoPlanBlackBoardControl;
import net.sf.xoperator.namespaces.NamespaceConf;
import net.sf.xoperator.ui.StringMessage;

public class ListNamespacesCommand extends Command {

   @Override
   public String getHelp() {
      return "lists all the defined namespaces";
   }

   @Override
   public String getKeyWord() {
      return "list ns";
   }

   @Override
   public List<Object> performInternal(String call, NoPlanBlackBoardControl control,
         Interactor interactor) {
      Set<Object> nsconfs = control.getConfigurationContext().blackboard()
            .readAll(new KindOfFilter(NamespaceConf.class));
      String returnmessage = "";

      
      for (Object object : nsconfs) {
         NamespaceConf conf = (NamespaceConf) object;
         if (!conf.isObsolete()) {
            returnmessage += conf.getPrefix() + ":" + conf.getUrl() + "\n";
         }

      }
      
      if (returnmessage.isEmpty()) {
         returnmessage = "No namespaces defined";
      } else {
         returnmessage =  "The defined namespaces are: \n" + returnmessage;
      }

      return Arrays.asList(new Object[] { new StringMessage(returnmessage) });
   }
   

	@Override
	public Pattern getValidationPattern() {

		return Pattern.compile(".*");
	}

}
